<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="" prop="beginTime">
        <el-date-picker clearable
                        v-model="queryParams.beginTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="开始日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item prop="endTimee">
        <el-date-picker clearable
                        v-model="queryParams.endTimee"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="结束日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="员工状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择员工状态" style="width: 100%" @keyup="handleQuery">
          <el-option label="--请选择--" value=""></el-option>
          <el-option label="试用" value="3"></el-option>
          <el-option label="正式" value="4"></el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="合同类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择合同类型" style="width: 100%" @keyup="handleQuery">
          <el-option label="--请选择--" value=""></el-option>
          <el-option label="劳务合同" value="劳务合同"></el-option>
          <el-option label="劳务派遣合同" value="劳务派遣合同"></el-option>
          <el-option label="实习合同" value="实习合同"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入搜索内容"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <!--    显示-->
    <el-table v-loading="loading" :data="contractList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="姓名" align="center" prop="name"/>
      <el-table-column label="岗位" align="center" prop="positionApplied"/>
      <el-table-column label="创建人" align="center" prop="uname"/>
      <el-table-column label="员工状态" align="center" prop="status">
        <template slot-scope="scope">
          <div v-if="scope.row.status != null && scope.row.status == '3'">
            试用
          </div>
          <div v-else-if="scope.row.status != null && scope.row.status=='4'">
            正式
          </div>
        </template>
      </el-table-column>
      <el-table-column label="合同类型" align="center" prop="type"/>
      <el-table-column label="合同状态" align="center" prop="contractStatus"/>
      <el-table-column label="合同期限" align="center" prop="contractPeriod"/>
      <el-table-column label="合同签署日期" align="center" prop="auditTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同到期日期" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否签约" align="center" prop="isContract">
        是
      </el-table-column>
      <el-table-column label="签约次数" align="center" prop="signingTimes"/>

      <el-table-column label="创建时间" align="center" prop="auditTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>


      <el-table-column label="操作" width="150" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleAdd(scope.row.id)"
            v-hasPermi="['system:contract:hetAdd']"
          >续签
          </el-button>

          <el-button
            size="mini"
            type="text"
            @click="playVideo(scope.row.id)"
            v-hasPermi="['system:contract:query']"
          >查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" :close-on-click-modal='false' append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="合同编号" prop="contractNo">
              <el-input @blur="setValue" onkeyup="this.value = this.value.replace(/[^\d.]/g,'');"
                        v-model="form.contractNo" placeholder="请输入合同编号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择合同类型" style="width: 100%">
                <el-option label="--请选择--" value=""></el-option>
                <el-option label="劳务合同" value="劳务合同"></el-option>
                <el-option label="劳务派遣合同" value="劳务派遣合同"></el-option>
                <el-option label="实习合同" value="实习合同"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="合同开始日期" prop="auditTime">
              <el-date-picker clearable
                              style="width: 100%"
                              v-model="form.auditTime"
                              type="date"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择合同签署日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同结束日期" prop="endTime">
              <el-date-picker clearable
                              style="width: 100%"
                              v-model="form.endTime"
                              type="date"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择合同到期日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="合同期限" prop="contractPeriod">
              <el-select v-model="form.contractPeriod" placeholder="请选择合同期限" style="width: 100%">
                <el-option label="--请选择--" value=""></el-option>
                <el-option label="1年" value="1年"></el-option>
                <el-option label="2年" value="2年"></el-option>
                <el-option label="3年" value="3年"></el-option>
                <el-option label="4年" value="4年"></el-option>
                <el-option label="5年" value="5年"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="合同执行状态" prop="contractStatus">
              <el-select v-model="form.contractStatus" placeholder="请选择合同执行状态" style="width: 100%">
                <el-option label="--请选择--" value=""></el-option>
                <el-option label="未执行" value="未执行"></el-option>
                <el-option label="执行中" value="执行中"></el-option>
                <el-option label="已到期" value="已到期"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="签署公司" prop="residentEnterprises">
              <el-input v-model="form.residentEnterprises" placeholder="请输入签署公司"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="12" style="float: left;padding-left: 20px;padding-bottom: 10px">
            <el-form-item label="合同附件" prop="filePath">
              <el-input   @select="parentIdChange"  v-model="form.filePath" />
              <el-upload
                ref="uploadRef"
                action=""
                accept=".pdf"
                :auto-upload="false"
                :on-change="handleChangeContract"
                :show-file-list="false">
                <el-button
                  type="warning"
                  size="mini"
                >上传合同附件
                </el-button>
                <span v-text="this.filePath"></span>
                <span v-if="pd">
                  <font color="#a52a2a">
                    {{mess}}
                  </font>
                </span>

              </el-upload>
            </el-form-item>

          </el-col>
        </el-row>


      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--查看对话框    -->

    <el-dialog :title="title"  :visible.sync="videoVisible" width="75%" :close-on-click-modal='false' append-to-body>
      <el-table v-loading="loading" :data="contractLists">
        <el-table-column type="selection" width="55" align="center"/>

        <el-table-column label="姓名" align="center" prop="name"/>
        <el-table-column label="岗位" align="center" prop="positionApplied"/>
        <!--<el-table-column label="驻场企业" align="center" prop="enterPrise"/>-->
        <el-table-column label="合同类型" align="center" prop="type"/>
        <el-table-column label="执行状态" align="center" prop="contractStatus"/>
        <el-table-column label="合同期限" align="center" prop="contractPeriod"/>
        <el-table-column label="合同签署日期" align="center" prop="auditTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="合同到期日期" align="center" prop="endTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-s-promotion"
              @click="download(scope.row.conId)"
            >合同下载
            </el-button>
          </template>
        </el-table-column>
      </el-table>


    </el-dialog>


  </div>
</template>

<script>
import {
  listContract,
  getContract,
  delContract,
  addContract,
  updateContract,
  hetAdd,

} from "@/api/system/contract";
import {uploadFile2} from '@/api/system/resume'
import {getStation} from "@/api/system/stations";

export default {
  /*conId:null,
    id: null,
    contractNo: null,
    type: null,
    auditTime: null,

    endTime: null,
    contractPeriod: null,
    contractStatus: null,
    residentEnterprises: null,
    filePath: null,*/
  name: "Contract",
  data() {
    return {
      // 遮罩层
      mess:null,
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      total1: 0,
      // 合同管理表格数据
      contractList: [],
      contractLists: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      videoVisible: false,
      filePath: null,
      // 合同详情
      querys: {
        pageNum: 1,
        pageSize: 10,
        enterPrise: null,
        conId: null,
        id: null,
        name: null,
        filePath: null,
        stationName: null,
        contractNo: null,
        residentEnterprises: null,
        staffStatus: null,
        type: null,
        contractStatus: null,
        contractPeriod: null,
        auditTime: null,
        endTime: null,
        createBy: null,
        updateBy: null,
        isContract: null,
        signingTimes: null,
        beginTime: null,
        endTimee: null,
        isDelete: null
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        stationName: null,
        contractNo: null,
        residentEnterprises: null,
        staffStatus: null,
        status: null,
        type: null,
        contractStatus: null,
        contractPeriod: null,
        auditTime: null,
        endTime: null,
        createBy: null,
        updateBy: null,
        isContract: null,
        signingTimes: null,
        beginTime: null,
        endTimee: null,
        isDelete: null
      },

      // 表单参数
      form: {},
      // 表单校验

      pd:false,

      rules: {
        contractNo: [
          {required: true, message: '合同编号不能为空', trigger: 'blur'}
        ],
        residentEnterprises: [
          {required: true, message: '签署公司不能为空', trigger: 'blur'}
        ],
        auditTime: [
          {required: true, message: '开始日期不能为空', trigger: 'blur'}
        ],
        endTime: [
          {required: true, message: '结束日期不能为空', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '类型不能为空', trigger: 'change'}
        ],
        contractPeriod: [
          {required: true, message: '合同期限不能为空', trigger: 'change'}
        ],
        contractStatus: [
          {required: true, message: '合同执行状态不能为空', trigger: 'change'}
        ],
      },

    };
  },
  created() {
    this.getList();
  },
  methods: {

    //文件下载

    download(conId) {
      this.$download.hetzip(conId)
    },

    /** 查询合同管理列表 */
    getList() {
      this.loading = true;
      listContract(this.queryParams).then(response => {
        this.contractList = response.rows;
        this.total = response.total;
      });
      this.loading = false;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.pd = false;
      this.filePath =null;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        stationName: null,
        status: null,
        contractNo: null,
        residentEnterprises: null,
        staffStatus: null,
        type: null,
        contractStatus: null,
        contractPeriod: null,
        auditTime: null,
        endTime: null,
        isContract: null,
        signingTimes: null,
        createTime: null,
        createBy: null,
        updateBy: null,
        updateTime: null,
        beginTime: null,
        endTimee: null,
        isDelete: null

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(id) {
      this.reset();

      this.form.id = id;

      this.open = true;
      this.title = "添加合同";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getContract(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改合同管理";
      });
    },
    /** 提交按钮 */


    submitForm() {
      if (this.filePath== null){
        this.pd = true;
        this.mess = '请上传附件文件'

      }else{
        this.pd =false;
      }


      this.$refs["form"].validate(valid => {


        if (this.pd == false){
          if (valid){

            hetAdd(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.filePath = null;
              this.getList();
            });
          }
        }

      });

      /* if (valid) {
         if (this.form.id != null) {
           updateContract(this.form).then(response => {
             this.$modal.msgSuccess("修改成功");
             this.open = false;
             this.getList();
           });
         } else {

         }
       }*/
      /*  });*/
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;

      this.$modal.confirm('是否确认删除合同管理编号为"' + ids + '"的数据项？').then(function () {
        return delContract(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/contract/export', {
        ...this.queryParams
      }, `contract_${new Date().getTime()}.xlsx`)
    },

    /** 详情按钮操作 */
    //点击按钮时,触发弹出效果
    playVideo(id) {
      this.querys.id = id;

      getContract(this.querys).then(response => {
        this.contractLists = response.rows;
        this.total1 = response.total;
        this.title = "已签合同";
        this.videoVisible = true

      });
    },

    /*文件上传*/
    handleChangeContract(file) {

      if (file != null) {
        let formData = new FormData();
        formData.append('file', file.raw)
        uploadFile2(formData).then(response => {
          this.filePath = response;
          this.form.filePath = this.filePath
          this.pd=false;
          /*this.msgSuccess("上传成功！");*/
        });
      }
    },
    setValue(e) {
      this.form.addOutputValue = e.target.value
    },
    parentIdChange(){
      this.$nextTick(()=>{
        this.$refs.form.validateField('filePath')
      })
    }


  }
};
</script>
